package com.jumi.microservice.controller.invoice.admin;

import com.alibaba.excel.EasyExcel;
import com.jumi.microservice.api.web.R;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ExportExcelCommonResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.dto.invoice.*;
import com.jumi.microservice.service.invoice.admin.InvoiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(tags = {"后台发票相关接口"})
@RestController
@RequestMapping("/admin/invoice")
public class AdminInvoiceController extends BaseController {
    @Resource
    private InvoiceService invoiceService;

    @GetMapping("/invoiceList")
    @ApiOperation("后端列表")
    TableDataInfo<InvoiceListResponse> invoiceList(InvoiceListRequest invoiceListRequest) {
        return invoiceService.commentsAdminList(invoiceListRequest);
    }

    @GetMapping("/invoiceDetail")
    @ApiOperation("后端开票信息")
    public ResponseResult<InvoiceInfoResponse> invoiceDetail(InvoiceInfoRequest invoiceInfoRequest) {
        ResponseResult<InvoiceInfoResponse> result = new ResponseResult<>();
        InvoiceInfoResponse invoiceInfoResponse = invoiceService.invoiceInfo(invoiceInfoRequest);
        result.setData(invoiceInfoResponse);
        return result;
    }

    @GetMapping("/invoiceView")
    @ApiOperation("后端发票查看")
    public ResponseResult<InvoiceViewResponse> invoiceView(InvoiceInfoRequest invoiceInfoRequest) {
        ResponseResult<InvoiceViewResponse> result = new ResponseResult<>();
        InvoiceViewResponse invoiceViewResponse = invoiceService.invoiceView(invoiceInfoRequest);
        result.setData(invoiceViewResponse);
        return result;
    }

    @PutMapping(value = "/editInvoice")
    @ApiOperation("后端发票提交")
    public ResponseResult<Boolean> editInvoice(InvoiceEditRequest invoiceEditRequest) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        result.setData(invoiceService.editInvoice(invoiceEditRequest));
        return result;
    }

    @PutMapping(value = "/invoiceRed")
    @ApiOperation("后端发票冲红")
    public ResponseResult<Boolean> invoiceRed(InvoiceRedRequest invoiceRedRequest) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        result.setData(invoiceService.invoiceRed(invoiceRedRequest));
        return result;
    }

    @PutMapping(value = "/invoiceBatchRed")
    @ApiOperation("后端发票批量冲红")
    public ResponseResult<Boolean> invoiceBatchRed(InvoiceBatchRedRequest invoiceBatchRedRequest) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        result.setData(invoiceService.invoiceBatchRed(invoiceBatchRedRequest));
        return result;
    }

    @PostMapping(value = "/invoiceEditStatus")
    @ApiOperation("后端订单发票状态修改")
    public Boolean invoiceEditStatus(InvoiceStatusRequest invoiceStatusRequest) {
        return invoiceService.invoiceEditStatus(invoiceStatusRequest);
    }


    /**
     * 发票状态 ： 0代开票，1 已开票，2 待更正，3 已更正，4 待冲红 5 已冲红，6 不可开票
     *
     * @param invoiceOneRequest
     * @return
     */
    @GetMapping("/invoiceOneStatus")
    @ApiOperation("根据订单编号查看发票状态")
    public InvoiceOneResponse invoiceOneStatus(InvoiceOneRequest invoiceOneRequest) {
        return invoiceService.invoiceOneStatus(invoiceOneRequest);
    }

    @ApiOperation("导出")
    @PostMapping("/exportExcel")
    public void exportExcel(InvoiceExplorRequest request, HttpServletResponse httpServerResponse) throws IOException {
        ExportExcelCommonResponse<InvoiceExplorResponse> exportExcelResponseExport = invoiceService.exportExcel(request);
        httpServerResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        httpServerResponse.setCharacterEncoding("utf-8");
        httpServerResponse.setHeader("Content-disposition", "attachment;filename=" + exportExcelResponseExport.getFileName());
        EasyExcel.write(httpServerResponse.getOutputStream(), InvoiceExplorResponse.class).sheet("sheet1").doWrite(exportExcelResponseExport.getData());
    }

//    @ApiOperation("第三方发票作废")
//    @PostMapping("/redInkOnInvoice")
//    public ResponseResult<String> redInkOnInvoice(InvoiceOneRequest request) {
//        return invoiceService.redInkOnInvoice(request);
//    }


    @ApiOperation("根据流水号查询")
    @PostMapping("/getInvoicePdf")
    public ResponseResult<String> getInvoicePdf(String serialNumber) {
        ResponseResult<String> responseResult = new ResponseResult<>();
        responseResult.setData(invoiceService.getInvoicePdf(serialNumber));
        return responseResult;
    }
}
